package com.demo.house.service.impl;


import com.demo.house.common.FileUtil;
import com.demo.house.dao.HouseUrlDao;
import com.demo.house.entity.HouseUrl;
import com.demo.house.service.HouseUrlService;
import com.demo.house.vo.response.ResponseUrlVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.Calendar;
import java.util.List;

@Service
public class HouseUrllmpl implements HouseUrlService {

    @Autowired
    private HouseUrlDao houseUrlDao;

    private final String uploadPath="D:/img/";

    @Override
    public List<HouseUrl> selectBll() {
        return houseUrlDao.selectBll();
    }
/*上传图片*/
    @Override
    public String up(Integer id, MultipartFile file) {
        //相对路径
        String relativePath  = "files" + File.separator + "img"  + File.separator;
        //绝对路径
        String absolutePath = uploadPath + relativePath;
        //执行文件上传
        File upload = FileUtil.upload(file, absolutePath);
        if (!ObjectUtils.isEmpty(upload)) {
            HouseUrl houseUrl = new HouseUrl();
            houseUrl.setHomeSourseId(id);
            houseUrl.setUrl(relativePath + upload.getName());
            houseUrlDao.insertSelective(houseUrl);
            return houseUrl.getUrl();
        }
        else {
            return null;
        }
    }
/*根据id查询*/
    @Override
    public HouseUrl selectUrl(Integer id) {
        System.out.println("这个id为:  "  + id);
        List<HouseUrl> houseUrl = houseUrlDao.selectUrl(id);

        System.out.println("查出的数据为:  "  + houseUrl);
        return houseUrl.get(0);
    }
    @Override
    public List<HouseUrl> selectUrl2(Integer id) {
        return houseUrlDao.selectUrl2(id);
    }

}
